package com.spzx.product.configure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        //处理器数量(核心线程数)
        int corePoolSize = Runtime.getRuntime().availableProcessors();
        //最大线程数
        int maximumPoolSize = corePoolSize * 2;
        // 保持活跃时间为 60 秒
        long keepAliveTime = 60L;
        return new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                // 使用有界队列控制任务数量
                new ArrayBlockingQueue<>(100),
                Executors.defaultThreadFactory(),
//               new RejectedExecutionHandler() {
//                   @Override
//                   public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
//                       executor.submit(r);
//                       }
//    }
                ((runnable, executor) -> executor.submit(runnable))

        );
    }
}
